/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import dto.LoaiDeThiDTO;

/**
 *
 * @author Stanley
 */
public class LoaiDeThiDAO {

    public ArrayList getAllLoaiDeThi() {
        Connection connection = SqlDatabase.getInstance();
        try {
            Statement query = (Statement) connection.createStatement();
            ResultSet rs = query.executeQuery("select * from loaidethi order by maLDT asc");
            ArrayList dsLoaiDeThi = new ArrayList();
            while (rs.next()) {
                LoaiDeThiDTO info = new LoaiDeThiDTO(
                        rs.getInt("maLDT"),
                        rs.getString("tenloai"),
                        rs.getInt("thoigian"),
                        rs.getInt("socaude"),
                        rs.getInt("socaukho"),
                        rs.getInt("socautb"));
                dsLoaiDeThi.add(info);
            }
            return dsLoaiDeThi;
        } catch (Exception e) {
            return null;
        }
    }

    public LoaiDeThiDTO getLoaiDeThi(String maLDT) {
        //query.executeQuery("select * from loaidethi where maLDT = " + maLDT);
        Connection connection = SqlDatabase.getInstance();
        try {
            Statement query = (Statement) connection.createStatement();
            ResultSet rs = query.executeQuery("select * from loaidethi where maLDT = " + maLDT);
            rs.next();
            LoaiDeThiDTO info = new LoaiDeThiDTO(
                    rs.getInt("maLDT"),
                    rs.getString("tenloai"),
                    rs.getInt("thoigian"),
                    rs.getInt("socaude"),
                    rs.getInt("socaukho"),
                    rs.getInt("socautb"));

            return info;
        } catch (Exception e) {
            return null;
        }
    }

    public int getLoaiDeThiLonNhat() {
        Connection connection = SqlDatabase.getInstance();
        try {
            Statement query = (Statement) connection.createStatement();
            String sql = "select max(maLDT) as max from loaidethi";
            ResultSet rs = query.executeQuery(sql);
            rs.next();
            return rs.getInt("max");
        } catch (Exception e) {
            return -1;
        }
    }

    public boolean themLoaiDeThi(LoaiDeThiDTO info) {
        Connection connection = SqlDatabase.getInstance();
        try {
            Statement query = (Statement) connection.createStatement();
            String sql = "insert into loaidethi (maLDT, tenloai, thoigian, socaude, socautb, socaukho) values ( "
                    + Integer.toString(info.getMaLDT()) + " , '" + info.getTenloai() + "' , "
                    + Integer.toString(info.getThoigian()) + " , " + Integer.toString(info.getSocaude()) + " , "
                    + Integer.toString(info.getSocautb()) + " , " + Integer.toString(info.getSocaukho()) + " )";
            int result = query.executeUpdate(sql);
            return result == 0 ? false : true;
        } catch (Exception e) {
            return false;
        }
    }
}
